//
//  JBWNavigationController.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/6/29.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

class JBWNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //重写设置手势代理
        interactivePopGestureRecognizer?.delegate = self
        
    }
    
    //返回
    @objc private func backClick(){
        popViewController(animated: true)
    }
    
    //监听边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return(childViewControllers.count != 1)
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0 {
            //设置默认值
            var title = "返回"
            //二级界面 显示上一级title 其余显示 返回
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.navigationItem.title ?? ""
                //隐藏tabBar
                viewController.hidesBottomBarWhenPushed = true
            }
            
            //如果自定义了左侧导航按钮 会导致右划手势失效
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(backClick))
            
        }
        //监听该方法完成左侧按钮设置
        super.pushViewController(viewController, animated: animated)
    }
}
